package org.mikrosopht.client;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;

public class MovieListSearcher extends Composite {
  private HorizontalPanel panel = new HorizontalPanel();
  private TextBox searchBox = new TextBox();
  private String currentSearch = "";
  private MovieListSearcherEventReceiver receiver;

  public MovieListSearcher(MovieListSearcherEventReceiver receiver) {
    this.receiver = receiver;
    Label searchLabel = new Label("Search:");
    panel.add(searchLabel);
    panel.add(searchBox);
    panel.setCellVerticalAlignment(searchLabel, HasVerticalAlignment.ALIGN_MIDDLE);
    searchBox.addKeyboardListener(new SearchBoxChangeHandler());
    initWidget(panel);
  }

  private class SearchBoxChangeHandler extends KeyboardListenerAdapter {
    public void onKeyUp(Widget sender, char keyCode, int modifiers) {
      String newSearch = ((TextBox) sender).getText();
      if (!newSearch.equals(currentSearch)) {
        currentSearch = newSearch;
        receiver.onTextChange(newSearch);
      }
    }
  }
}
